package com.itcast.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itcast.yygh.hosp.repository.HospitalRepository;
import com.itcast.yygh.hosp.service.HospitalService;
import com.itcast.yygh.model.enums.DictEnum;
import com.itcast.yygh.model.model.hosp.Hospital;
import com.itcast.yygh.model.vo.hosp.HospitalQueryVo;
import com.itcast.yygh.cmn.client.DictFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> map) {
        //1.从map中获取hospital对象的数据
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(map), Hospital.class);
        //2.根据医院的编码查询mongo库
        Hospital newHospital = hospitalRepository.findByHoscode(hospital.getHoscode());
        //3.如果查询到的对象不为空 就执行更新操作
        if(Objects.isNull(newHospital)){
            //执行添加操作
            //上线的状态
            hospital.setStatus(0);
            //添加时间
            hospital.setCreateTime(new Date());
            //更新时间
            hospital.setUpdateTime(new Date());
            //是否删除
            hospital.setIsDeleted(0);
            //执行保存
            hospitalRepository.save(hospital);
        }else{
            //4.如果查询到的对象为空 就执行保存操作
            //执行更新操作
            //上线的状态
            hospital.setStatus(newHospital.getStatus());
            //创建时间
            hospital.setCreateTime(newHospital.getCreateTime());
            //更新时间
            hospital.setUpdateTime(newHospital.getUpdateTime());
            //对象的id
            hospital.setId(newHospital.getId());
            //是否删除
            hospital.setIsDeleted(newHospital.getIsDeleted());
            //执行保存方法
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        //直接查询mongodb中的医院数据
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> findPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //1.构建排序规则
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //2.创建pageable的实例
        Pageable pageable = PageRequest.of(page-1,limit,sort);
        //3.设置查询条件
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //4.创建条件构造器
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true);
        //5.创建条件example实例
        Example<Hospital> example = Example.of(hospital,matcher);
        //6.进行分页的数据查询
        Page<Hospital> pages = hospitalRepository.findAll(example,pageable);
        //7.返回查询后的结果
        //8.封装数据字典的数据
        //封装医院等级数据
        pages.getContent().stream().forEach(item -> {
            this.packHospital(item);
        });
        return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        //判断状态中的值是否符合要求
        if(status.intValue() ==0 || status.intValue()==1){
            //先根据id进行数据查询
            Hospital hospital = hospitalRepository.findById(id).get();
            if(!Objects.isNull(hospital)){
                hospital.setStatus(status);
                hospital.setUpdateTime(new Date());
                hospitalRepository.save(hospital);
            }
        }
    }

    @Override
    public Map<String,Object> show(String id) {
        Map<String,Object> map = new HashMap<>();
        //根据id查询医院详情数据
        Hospital hospital = hospitalRepository.findById(id).get();
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        //把医院对象中规则设置成null,不需要重复的设置
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        //调用查询方法
        List<Hospital> hospitals = hospitalRepository.findByHosnameLike(hosname);
        return hospitals;
    }

    @Override
    public Map<String, Object> findHospDetail(String hoscode) {
        Map<String,Object> map = new HashMap<>();
        //根据id查询医院详情数据
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        //把医院对象中规则设置成null,不需要重复的设置
        hospital.setBookingRule(null);
        return map;
    }

    /**
     * 封装数据
     * @param hospital
     * @return
     */
    private Hospital packHospital(Hospital hospital) {
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(),hospital.getHostype());
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString + hospital.getAddress());
        return hospital;
    }
}
